package com.kingtone.rongji.tool;

import org.apache.http.HttpEntity;

import android.content.Context;
import android.text.TextUtils;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.BaseJsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class AppRestClient {
	private static final String TAG = "AppRestClient";
	public static final boolean isDevelop = true; 
	
	public static final int PAGE_SIZE = 20;
	public static String BASE_URL = "http://dev.rongji365.com";// 线上环境
	public static String BASE_HOME_IMG_URL = "http://dev.rongji365.com/Storage/Banner/";// 线上环境
	public static final String BASE_PRODUCT_IMG_URL = "http://dev.rongji365.com/Storage/PrdImage/ProductImage/";// 线上环境
	public static final String BASE_PRODUCT_DESCRIPTION_URL = "http://m.rongji365.com/Product/ProductDescriptionForApp?mode=1&productId=";// 线上环境
	public static final String BASE_CATEGORY2_URL = "http://m.rongji365.com/Themes/RongJi/Style/base/imgs/sort/category/";// 分类图片地址 
	public static final String BASE_CATEGORY3_URL = "http://m.rongji365.com/Themes/RongJi/Style/base/imgs/sort/subcategory/";// 分类图片地址
	public static final String PRODUCT_DETAIL_URL = "http://m.rongji365.com/Product/ProductDetail?prd=";// 分享产品地址 
	public static final String FEEDBACK_URL = "http://m.rongji365.com/Customer/FeedbackSubmit";
	public static final String HELP_URL = "http://m.rongji365.com/Content/ContentInfo?mode=1";
	public static String WEBSITE_URL = "http://m.rongji365.com";
	public static final String CUSTOMER_PHONE = "4008804365";
	public static String API_KEY = "01FC34072F072EDB5322FE7E2E36E60FEA0675DD";
	
	static {
		if(isDevelop) {
			BASE_URL = "http://rongadmin.pupuxing.net";// 开发环境
			BASE_HOME_IMG_URL = "http://rongadmin.pupuxing.net/Storage/Banner/";// 开发环境
			WEBSITE_URL = "http://rong.pupuxing.net";
			API_KEY = "406FF6B6BB2B910B71F99209B1FFEEB6A8F0FA62";
		}
	}

	private static AsyncHttpClient client = new AsyncHttpClient();

	public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
		client.get(getAbsoluteUrl(url), params, responseHandler);
	}

	public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
		client.post(getAbsoluteUrl(url), params, responseHandler);
	}
	
	public static void get(String url, RequestParams params, BaseJsonHttpResponseHandler responseHandler) {
		client.get(getAbsoluteUrl(url), params, responseHandler);
	}

	public static void post(String url, RequestParams params, BaseJsonHttpResponseHandler responseHandler) {
		client.post(getAbsoluteUrl(url), params, responseHandler);
	}
	
	public static void postEntity(Context context, String url, HttpEntity entity, BaseJsonHttpResponseHandler responseHandler) {
		client.post(context, getAbsoluteUrl(url), entity, "application/json", responseHandler);
	}

	private static String getAbsoluteUrl(String relativeUrl) {
		return BASE_URL + relativeUrl;
	}
	

	// 主页面图片地址处理
	public static String convertImageUrl(String productId, String productName) {
		if (null != productId && !TextUtils.isEmpty(productId)) {
			return AppRestClient.BASE_PRODUCT_IMG_URL + productId.substring(0, 3) + "/" + productId + "/p800/" + productName;
		}
		return null;
	}
	
}
